package com.zebra.gateway;


import com.zebra.gateway.processor.GatewayProcessorUtil;
import com.zebra.remoting.config.ServerInfoMessage;
import com.zebra.remoting.protocol.CommandProfile;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GatewayStartAfterInitialize {
    private static final InternalLogger logger = InternalLoggerFactory.getInstance(GatewayStartAfterInitialize.class);
    /**
     * 启动之后 需要向注册中心发送 请求 【注册中心向GW发送消息注册】；
     * 返回，CMD list； TMS_INFO_MSG list；
     * GatewayServerInfo ： serverId 更新本地缓存
     *
     * 将连接TMS 信息 返回给注册中心 然后更新到redis
     *
     * run tms server
     * @param commandProfiles   调用前从注册中心获取的 cmds
     * @param tmsList  tms 列表 如果没有 tms 列表则 启动失败，未获取到任何 可用TMS
     * @throws Exception
     */
    public void start(List<CommandProfile> commandProfiles, List<ServerInfoMessage> tmsList, int serverId) throws Exception {

        if (serverId == 0){
            throw new Exception("Start after: Register GatewayServerInfo serverId is 0");
        }

        if (null == commandProfiles || commandProfiles.isEmpty()){
            throw new Exception("Start after: CommandProfiles is null");
        }

        if (null == tmsList || tmsList.isEmpty()){
            throw new Exception("Start after: TMS list is null");
        }
        //serverId
        GwConfigInfoManager.getInstance().setGwServerId(serverId);

        //CMD
        GwConfigInfoManager.getInstance().registerCMDS(commandProfiles);

        //connection tms
        GatewayProcessorUtil.sendConnectionToTMS(tmsList);

        if (GwConfigInfoManager.getInstance().getRaSize() < 1){
            throw new Exception("Register size is null");
        }

        OnlineNumberService onlineNumberService = new OnlineNumberService();
        onlineNumberService.start();
        logger.info("start after initialize success ");
        GatewayProcessorUtil.printLog();

    }



}
